package cn.edu.suda.ada.database;
import cn.edu.suda.ada.database.dao.AdvModel;
import cn.edu.suda.ada.database.exception.DatabaseException;
import cn.edu.suda.ada.database.objects.Column;
import cn.edu.suda.ada.database.objects.Key;
import cn.edu.suda.ada.database.objects.Key.Method;
import cn.edu.suda.ada.database.objects.Table;

import java.util.*;

/**
 * 高级示例
 * 
 * @author JiangJun
 *
 */
public class AdvDemo {
	public static void main(String[] args) {
		Table table = new Table();
		table.setName("test");
		
		Column<Integer> col1 = new Column<Integer>("id", "int", 10);
		col1.setComment("测试键");
		Column<String> col2 = new Column<String>("value", Column.Type.STRING, 200);
		col2.setDefaultVal("默认值");
		col2.setEmpty(false);
		col2.setComment("测试值");
		
		table.addColumn(col1);
		table.addColumn(col2);

		Key pk = new Key();
		pk.addColumn(col1.getName());
		
		Key unique = new Key();
		unique.setName("unique_keys");
		unique.setType(Key.Type.UNIQUE);
		unique.setMethod(Method.HASH);
		unique.addColumn(col1.getName());
		unique.addColumn(col2.getName());
		
		table.addKey(pk);
		table.addKey(unique);
		
		try {
			AdvModel model = new AdvModel();
			model.setTableName("test2");
			model.drop();
			model.setTableName("test3");
			model.drop();
			model.setTable(table);
			
			// 删除、创建、是否存在
			System.out.println(model.drop());
			System.out.println(model.create());
			System.out.println(model.exist());
			
			// 单条插入
			Map<String, Object> data = new HashMap<String, Object>();
			data.put("id", "11");
			data.put("value", "test11");
			System.out.println(model.insert(data));
			System.out.println(model.select());
			
			// 批量插入
			List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
			Map<String, Object> data1 = new HashMap<String, Object>();
			data.clear();
			data1.put("id", 1);
			data1.put("value", "test1");
			dataList.add(data1);
			Map<String, Object> data2 = new HashMap<String, Object>();
			data2.put("id", 2);
			data2.put("value", "test2");
			dataList.add(data2);
			System.out.println(model.insertAll(dataList));
			System.out.println(model.select());
			
			// 删除数据
			model.where("id", 11);
			System.out.println(model.delete());
			
			// 更新数据
			model.where("id", 11);
			data.put("value", "testUpdate");
			System.out.println(model.update(data));
			
			// 列信息
			Set<Column<?>> columns = model.columns();
			System.out.println(columns);
			
			// 修改列信息
			Column<String> newCol2 = new Column<String>("user1", "varchar(200)");
			System.out.println(model.alterColumn(col2, newCol2));
			
			// 新增列信息
			Column<String> col3 = new Column<String>("user2", "varchar(200)");
			System.out.println(model.addColumn(col3));
			
			// 删除列信息
			System.out.println(model.dropColumn(col3.getName()));
			
			// 拷贝表信息
			System.out.println(model.copyTable("test2"));
			
			// 拷贝表结构信息
			System.out.println(model.copyTableStruct("test3"));
			
			// 数据列个数
			System.out.println(model.count());
			
			System.out.println(model.getSql());
			
			// 获得所有列名
			System.out.println(model.fields());
			
			// 获得所有索引
			System.out.println(model.keys());
			
			// 获取数据库表结构
			System.out.println(model.table());
		} catch (DatabaseException e) {
			System.err.println(e);
		}
	}
}
